package com.changgou.backend.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.feign.TemplateFeign;
import com.changgou.goods.pojo.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping("/wtemplate")
public class TemplateController {

    @Autowired
    private TemplateFeign templateFeign;

    @GetMapping("/toSpecAndPara")
    public String toGoods(Model model){
        return "specAndPara";
    }

    //查询规格参数类型
    @PostMapping("/search")
    @ResponseBody
    public Result findPage(@RequestBody Map<String,String> map){
        String page = map.get("page");
        String size = map.get("size");
        map.remove("page");
        map.remove("size");
        PageResult pageResult = templateFeign.findPage(map, Integer.parseInt(page), Integer.parseInt(size)).getData();
        return new Result(true, StatusCode.OK,"查询规格参数类型成功",pageResult);
    }

    //添加规格参数类型
    @PostMapping
    @ResponseBody
    public Result add(@RequestBody Template template){
        templateFeign.add(template);
        return new Result(true,StatusCode.OK,"添加规格参数类型成功");
    }

    //根据id查询规格参数类型 回显数据
    @GetMapping("/{id}")
    @ResponseBody
    public Result<Template> findById(@PathVariable("id") Integer id){
        Template template = templateFeign.findById(id).getData();
        return new Result(true,StatusCode.OK,"根据id查询规格参数类型成功",template);
    }

    //修改规格参数类型
    @PutMapping
    @ResponseBody
    public Result update(@RequestBody Template template){
        templateFeign.update(template,template.getId());
        return new Result(true,StatusCode.OK,"修改规格参数类型成功");
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public Result delete(@PathVariable("id") Integer id){
        templateFeign.delete(id);
        return new Result(true,StatusCode.OK,"删除成功");
    }

//    @GetMapping("/toSpec")
//    public String toSpec(Model model,@RequestParam("id")Integer id,@RequestParam("name")String name){
//        model.addAttribute("id",id);
//        model.addAttribute("name",name);
//        return "spec";
//    }
//
//    @GetMapping("/toPara")
//    public String toPara(Model model,@RequestParam("id")Integer id,@RequestParam("name")String name){
//        model.addAttribute("id",id);
//        model.addAttribute("name",name);
//        return "para";
//    }
}
